package com.violet.config;

import com.violet.interceptors.AppUserInterceptor;
import com.violet.interceptors.ManagerAdminInterceptor;
import com.violet.interceptors.PreInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders(HttpHeaders.SET_COOKIE)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有 /app 路径下的请求
        registry.addInterceptor(new PreInterceptor())
                .addPathPatterns("/**");

        // /app 登录拦截器
        registry.addInterceptor(new AppUserInterceptor())
                .addPathPatterns("/app/**");


        // /manager 登录拦截器
        registry.addInterceptor(new ManagerAdminInterceptor())
                .addPathPatterns("/manager/**");
    }
}